package com.basic.business.upDate.controller;


import com.basic.business.upDate.domain.TGoodsUpDate;
import com.basic.business.upDate.dto.TGoodsUpDateDto;
import com.basic.business.upDate.service.ITGoodsUpDateService;
import com.basic.common.annotation.FileExport;
import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.enums.BusinessType;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import com.basic.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 商品上新日期管理Controller
 *
 * @author zhaoxy_jn@163.com
 */
@RestController
@RequestMapping("/business/upDate")
public class TGoodsUpDateController extends BaseController
{
    @Resource
    private ITGoodsUpDateService tGoodsUpDateService;

    /**
     * 查询商品上新日期管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:upDate:list')")
    @GetMapping("/list")
    public TableDataInfo list(TGoodsUpDate tGoodsUpDate)
    {
        startPage();
        List<TGoodsUpDate> list = tGoodsUpDateService.getList(tGoodsUpDate);
        return getDataTable(list);
    }

    /**
     * 导出商品上新日期管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:upDate:export')")
    @Log(title = "商品上新日期管理", businessType = BusinessType.EXPORT)
    @FileExport(fileName = "商品上新日期管理.xlsx")
    @PostMapping("/export")
    public String export(TGoodsUpDate tGoodsUpDate) throws IOException
    {
        List<TGoodsUpDate> list = tGoodsUpDateService.getList(tGoodsUpDate);
        return EasyExcelUtils.excelExport(TGoodsUpDate.class,"商品上新日期管理数据", list);
    }

    /**
     * 获取商品上新日期管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:upDate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tGoodsUpDateService.get(id));
    }

    /**
     * 新增商品上新日期管理
     */
    @PreAuthorize("@ss.hasPermi('business:upDate:add')")
    @Log(title = "商品上新日期管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TGoodsUpDate tGoodsUpDate)
    {
        return toAjax(tGoodsUpDateService.insert(tGoodsUpDate));
    }

    /**
     * 修改商品上新日期管理
     */
    @PreAuthorize("@ss.hasPermi('business:upDate:edit')")
    @Log(title = "商品上新日期管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGoodsUpDate tGoodsUpDate)
    {
        return toAjax(tGoodsUpDateService.update(tGoodsUpDate));
    }

    /**
     * 删除商品上新日期管理
     */
    @PreAuthorize("@ss.hasPermi('business:upDate:remove')")
    @Log(title = "商品上新日期管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tGoodsUpDateService.deleteByIds(ids));
    }

    /**
     * 上传商品上新日期数据
     * @param file
     * @return
     */
    @PostMapping("/uploadUpNewDate")
    public AjaxResult uploadUpNewDate(MultipartFile file) {
        ExcelUtil<TGoodsUpDate> util = new ExcelUtil<TGoodsUpDate>(TGoodsUpDate.class);

        try {
            List<TGoodsUpDate> list = util.importExcel(file.getInputStream());
            if (list != null && !list.isEmpty()) {
                tGoodsUpDateService.batchInsertOrUpdate(list);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return AjaxResult.success();
    }
}
